package com.example.demo.web;

import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import com.example.demo.statemachine.Events;
import com.example.demo.statemachine.States;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class HomeController {

    @Autowired(required = false)
    private StateMachine<States,Events> stateMachine;

    @Autowired
    private UserService userService;

    public HomeController(StateMachine<States, Events> stateMachine,UserService userService) {
        this.stateMachine = stateMachine;
        this.userService = userService;
    }


    @ApiOperation(value = "state change test")
    @RequestMapping(value = "/s",method = RequestMethod.GET)
    @ResponseBody
    public String hello(){

        stateMachine.start();
        stateMachine.sendEvent(Events.EVENT1);
        stateMachine.sendEvent(Events.EVENT2);
        return "hello";

    }

    @ApiOperation(value = "save user",notes = "by path /name")
    @RequestMapping(value = "/login/{name}",method = RequestMethod.GET)
    @ResponseBody
    public String save(@PathVariable(name = "name")String name){

        User user = new User();
        user.setName(name);
        user.setAge(20);

        userService.saveUser(user);

        return "ok";
    }

    @ApiOperation(value = "show all user in page")
    @RequestMapping(value = "/ua",method = RequestMethod.GET)
    @ResponseBody
    public List<User> showUser(){
        return userService.findAllUser();
    }

}
